<template>
  <el-container class="home_container">
    <!--头部区域-->
    <el-header>
      <div>
        <img class="logo_img" src="../assets/logo.png"/>
        <span>后台管理系统</span>
      </div>
      <el-button @click="logout()" type="success">退出</el-button>
    </el-header>
    <el-container>
      <!--侧边栏（菜单栏）-->
      <el-aside :width="isCollapse ? '64px':'300px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu class="el-menu-vertical" unique-opened :collapse="isCollapse" router :default-active="activePath">
          <!--一级菜单-->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span v-text="item.authName"></span>
            </template>
            <!--二级菜单-->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveActivePath('/' + subItem.path)">
              <i class="el-icon-location"></i>
              <span v-text="subItem.authName"></span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <!--页面主体-->
        <el-main>
          <router-view></router-view>
        </el-main>
        <!--页面底部-->
        <el-footer>Copyright © 2013-2021 菜鸟教程  runoob.com All Rights Reserved. 备案号：闽ICP备15012807号-1</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created () {
    this.getAllMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data () {
    return {
      menuList: [],
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getAllMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('无法获取菜单')
      console.log(res)
      this.menuList = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveActivePath (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>

<style lang="less" scoped>
  .home_container{
    height: 100%;
    .el-container{
      height: 100%;
      .el-main{
        background-color: #c8ebdf;
      }
      .el-footer{
        background-color: #c8ebdf;
      }
    }
    .el-header{
      background-color: #bbe6d6;
      display: flex;
      padding-left: 0;
      justify-content: space-between;
      align-items: center;
      font-size: 25px;
      > div{
        display: flex;
        align-items: center;
      }
    }
  }
  .el-aside{
    background-color: #d9f0e6;
    box-shadow: 2px 10px 10px #c4c4c4;
  }
  .logo_img {
    width: 60px;
    height: 60px;
  }
  .el-menu-vertical{
    width: 100%;
    background-color: #d9f0e6;
    text-color: #000000;
    active-text-color: #03c2e6;
    text-align: left;
    .el-menu-item{
      background-color: #dff6ec;
    }
  }
  .toggle-button{
    cursor: pointer;
  }
</style>
